package com.hxkj.service.article.impl;

import com.hxkj.client.service.AnalyzeLeafletServiceClient;
import com.hxkj.client.service.GeneratorStaticFileServiceClient;
import com.hxkj.client.service.LeafletServiceClient;
import com.hxkj.domain.enums.LeafletExceptionEnum;
import com.hxkj.domain.po.Leaflet;
import com.hxkj.service.article.LeafletService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 用户service
 * <p>
 * Create by zzx on 2018/5/23 15:19
 */
@Service
@Slf4j
public class LeafletServiceImpl implements LeafletService {

    @Autowired
    private LeafletServiceClient leafletServiceClient;

    @Autowired
    private GeneratorStaticFileServiceClient generatorStaticFileServiceClient;

    /**
     * 获取单页list集合 带分页
     *
     * @param leaflet
     * @param pageParam
     * @return
     * @throws Exception
     */
    @Override
    public ResultPageStruct<Leaflet> getLeafletList(Leaflet leaflet, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ResultPageStruct<Leaflet> result = new ResultPageStruct<>();
        ParameterDataStruct<Leaflet> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(leaflet);
        parameterDataStruct.setPageTo(pageParam);
        if (leaflet.getSiteId() == null) {
            throw new CustomException(LeafletExceptionEnum.LEALFET_SITEID_NULL);
        }
        if (leaflet.getCreateTimeStart() == null && leaflet.getCreateTimeEnd() != null) {
            throw new CustomException(LeafletExceptionEnum.LEALFET_CREATETIMESTRAT_NULL);
        }
        if (leaflet.getCreateTimeStart() != null && leaflet.getCreateTimeEnd() == null) {
            throw new CustomException(LeafletExceptionEnum.LEALFET_CREATETIMESEND_NULL);
        }
        //将前端传过来的单页创建结束时间 + 一天减一秒
        if (leaflet.getCreateTimeEnd() != null) {
            leaflet.setCreateTimeEnd(leaflet.getCreateTimeEnd() + 24 * 60 * 60 * 1000 - 1);
        }

        try {
            //查询单页列表
            result = leafletServiceClient.getLeafletList(parameterDataStruct);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取单页列表失败:{}", e.getMessage());

        }
        result.setCode(null);
        result.setMessage(null);
        return result;
    }

    /**
     * 获取单条单页
     *
     * @param leaflet
     * @return
     * @throws Exception
     */
    @Override
    public Leaflet getLeaflet(Leaflet leaflet) throws Exception {
        ResultDataStruct<Leaflet> result = new ResultDataStruct();
        if (leaflet.getLeafletId() == null) {
            throw new CustomException(LeafletExceptionEnum.LEALFET_ID_NULL);
        }
        try {
            result = leafletServiceClient.getLeaflet(leaflet);
            if (result.getCode() != null) {
                throw new CustomException(LeafletExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询单页失败:{}", e);
            throw e;
        }
        return result.getData();
    }

    /**
     * 更新单页
     *
     * @param leaflet
     * @throws Exception
     */
    @Override
    public void updateLeaflet(Leaflet leaflet) throws Exception {
        ResultMessageStruct result;

        if (leaflet.getLeafletId() == null) {
            throw new CustomException(LeafletExceptionEnum.LEALFET_ID_NULL);
        }
        if (StringUtils.isEmpty(leaflet.getHeadline())) {
            throw new CustomException(LeafletExceptionEnum.LEALFET_HEADLINE_NULL);
        }
        if (StringUtils.isEmpty(leaflet.getDetail())) {
            throw new CustomException(LeafletExceptionEnum.AUTHROLE_PARTICULARS_NULL);
        }
        if (leaflet.getTempletId() == null) {
            throw new CustomException(LeafletExceptionEnum.AUTHROLE_TEMPLATEID_NULL);
        }
        leaflet.setUpdateTime(System.currentTimeMillis());

        //修改单页
        result = leafletServiceClient.updateLeaflet(leaflet);
        if (result.getCode() != 200) {
            throw new CustomException(LeafletExceptionEnum.getEnumByCode(result.getCode()));
        }
        CreateLeafletPages(leaflet);

    }

    /**
     * 删除单页,根据ids字符串,删除多条
     *
     * @param leaflet
     * @throws Exception
     */
    @Override
    public void removerLeaflet(Leaflet leaflet) throws Exception {
        ResultMessageStruct result;
        if (StringUtils.isBlank(leaflet.getLeafletIds())) {
            throw new CustomException(LeafletExceptionEnum.LEALFET_ID_NULL);
        }
        try {
            //单页删除
            result = leafletServiceClient.removerLeaflet(leaflet);
            if (result.getCode() != null) {
                throw new CustomException(LeafletExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 保存单页
     *
     * @param leaflet
     * @throws Exception
     */
    @Override
    public void saveLeaflet(Leaflet leaflet) throws Exception {
        ResultDataStruct<Leaflet> result;

        if (leaflet.getHeadline() == null) {
            throw new CustomException(LeafletExceptionEnum.LEALFET_HEADLINE_NULL);
        }
        if (leaflet.getDetail() == null) {
            throw new CustomException(LeafletExceptionEnum.AUTHROLE_PARTICULARS_NULL);
        }
        if (leaflet.getTempletId() == null) {
            throw new CustomException(LeafletExceptionEnum.AUTHROLE_TEMPLATEID_NULL);
        }
        leaflet.setCreateTime(System.currentTimeMillis());
        leaflet.setUpdateTime(System.currentTimeMillis());


        try {
            //新增单页
            result = leafletServiceClient.saveLeaflet(leaflet);
            if (result.getCode() != 200) {
                throw new CustomException(LeafletExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(LeafletExceptionEnum.LEALFET_RIGHT_ERRO);
        }

        CreateLeafletPages(result.getData());

    }


    /**
     * 生成多个单页的静态 chenfangzhou
     * 利用MQ异步执行
     *
     * @param leaflet
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct CreateLeafletPages(Leaflet leaflet) {
        ResultMessageStruct resultMessageStruct = generatorStaticFileServiceClient.GenLeafletFile(leaflet);
        return resultMessageStruct;
    }

    /**
     * 获取所有单页,不带分页
     *
     * @param leaflet
     * @return
     * @throws Exception
     */

    @Override
    public List<Leaflet> getLeafletList(Leaflet leaflet) throws Exception {
        ResultDataStruct<List<Leaflet>> result = leafletServiceClient.getLeafletList(leaflet);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getCode(), result.getMessage());
        }
        return result.getData();
    }
}
